<template>
	<view class="container">
		<!-- 状态提示 -->
		<view class="status-processing">
			<view class="status-icon">
				<image src="/static/check-mark4.png" class="tools-icon" />
			</view>
			<text class="status-text">处理中</text>
		</view>
		<!-- 进度条 -->
		<view class="progress-bar">
			<view class="progress-item">
				<view class="progress-icon completed">
					<image src="/static/check-mark2.png" class="check-mark2" />
				</view>
				<text class="progress-text">待派工</text>
			</view>
			<view class="progress-line completed"></view>
			<view class="progress-item">
				<view class="progress-icon completed">
					<image src="/static/check-mark2.png" class="check-mark2" />
				</view>
				<text class="progress-text">待接单</text>
			</view>
			<view class="progress-line completed"></view>
			
			<view class="progress-item">
				<view class="progress-icon completed">
					<image src="/static/check-mark2.png" class="check-mark2" />
				</view>
				<text class="progress-text">处理中</text>
			</view>
			<view class="progress-line"></view>
			
			<view class="progress-item">
				<view class="progress-icon">
					<image src="/static/check-mark2.png" class="check-mark2" />
				</view>
				<text class="progress-text">已完成</text>
			</view>
		</view>
		
		<!-- 选项卡 -->
		<view class="tabs">
			<view class="tab-item" :class="{ active: activeTab === 0 }" @click="activeTab = 0">
				<text>报修详情</text>
				<view class="tab-line" v-if="activeTab === 0"></view>
			</view>
			<view class="tab-item" :class="{ active: activeTab === 2 }" @click="activeTab = 2">
				<text>处理记录</text>
				<view class="tab-line" v-if="activeTab === 2"></view>
			</view>
		</view>
		<!-- 内容区域 -->
		<scroll-view class="content" scroll-y>
			<!-- 报修详情 -->
			<view v-if="activeTab === 0 && detail" class="repair-detail">
				<view class="detail-item">
					<text class="detail-label">报修类型：</text>
					<text class="detail-value">{{ detail.repair_type }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">故障描述：</text>
					<text class="detail-value">{{ detail.repair_content }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">报修时间：</text>
					<text class="detail-value">{{ detail.sub_date }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">联系方式：</text>
					<text class="detail-value">{{ detail.sub_name }} {{ detail.sub_tel }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">报修地点：</text>
					<text class="detail-value">{{ detail.repair_address }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">处理人员：</text>
					<text class="detail-value">{{ detail.handler_name }}</text>
				</view>
				<view class="detail-item">
					<text class="detail-label">开始处理：</text>
					<text class="detail-value">{{ detail.hand_date || '2025-6-6 14:15:00' }}</text>
				</view>
				<!-- <view class="detail-item">
					<text class="detail-label">预计完成：</text>
					<text class="detail-value">{{ detail.expectedTime || '2025-6-6 15:00:00' }}</text>
				</view> -->
			</view>
			
			<!-- 处理记录 -->
			<view v-if="activeTab === 2" class="processing-record">
				<!-- 开始现场维修工作 -->
<!-- 				<view class="record-item" v-if="detail.end_date">
					<text class="record-time">{{detail.end_date}}</text>
					<text class="record-action">开始现场维修工作</text>
				</view>
				<view class="record-item" v-if="detail.end_date">
					<text class="record-time">{{detail.end_date}}</text>
					<text class="record-action">到达现场，开始检查故障</text>
				</view> -->
				<view class="record-item" v-if="detail.hand_date">
					<text class="record-time">{{detail.hand_date}}</text>
					<text class="record-action">接到派工通知，准备前往现场</text>
				</view>
				<view class="record-item" v-if="detail.hand_date && detail.handler_name">
					<text class="record-time">{{detail.hand_date}}</text>
					<text class="record-action">派工给维修部{{detail.handler_name}}</text>
				</view>
				<view class="record-item" v-if="detail.sub_date">
					<text class="record-time">{{detail.sub_date}}</text>
					<text class="record-action">用户提交报修申请</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script lang="ts" setup>
	import { ref, onMounted } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import {usePublicStore} from '../../../store/public'

	const userPublic = usePublicStore();
	const activeTab = ref(0)
	const detail = ref<any>(null)
	const app = getApp();

	onLoad((options) => {
		// const id = options.id
		// // 从本地存储读取最新的orders数据
		// const orders = uni.getStorageSync('orders') || [];
		// detail.value = orders.find(item => item.repair_id == id)
		detail.value =  uni.getStorageSync("junmp_order") || [];
		uni.removeStorageSync('junmp_order');
	})
	
	const assignWorker = () => {
		uni.showModal({
			title: '确认操作',
			content: '确定要将此工单设为完成吗？',
			success: (res) => {
				if (res.confirm) {
					uni.showToast({
						title: '已设为完成',
						icon: 'success'
					});
					// 这里可以调用API进行派工
					const baseUrl = app.globalData.baseUrl;
						uni.request({
						    url: encodeURI(baseUrl + '/repair/com'),
							method:'POST',
						    data: {
								repair_id: detail.value.repair_id,
								handler_id: userPublic.getUserInfo().id,
								handler_name: userPublic.getUserInfo().name,
								status:'done'
						    },
						    header:{
								"X-Tenant-Id": uni.getStorageSync("tenantId"),
								"token" : uni.getStorageSync("token")
							},
						    success: (res) => {
								console.log(res.data)
						    }
						});
				}
			}
		});
	};
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		background: #F5F5F5;
		height: 100vh;
	}

	/*状态提示*/
	.status-processing {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 48rpx;
		background: linear-gradient(to right, #4095E5, #2E7BB8);
	}

	.status-icon {
		width: 68rpx;
		height: 68rpx;
		background: #fff;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
		margin-bottom: 16rpx;
	}

	.status-text {
		font-size: 35rpx;
		color: #fff;
	}

	.tools-icon {
		width: 47rpx;
		height: 47rpx;
	}

	/*进度条*/
	.progress-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 40rpx 60rpx;
		background-color: #fff;
	}

	.progress-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;
		z-index: 1;
	}

	.progress-icon {
		width: 45rpx;
		height: 45rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #ddd;
		margin: 0 15rpx;
	}

	.progress-icon.completed {
		background-color: #4095E5;
	}

	.progress-icon.current {
		background-color: #4095E5;
	}

	.progress-text {
		font-size: 15px;
		color: #737373;
		margin-top: 12rpx;
		white-space: nowrap;
	}

	.progress-line {
		flex: 1;
		height: 2rpx;
		position: relative;
		top: -20rpx;
		background-color: #ddd;
	}

	.progress-line.completed {
		background-color: #AEADAE;
	}
	
	.check-mark2{
		width: 30rpx;
		height: 30rpx;
	}
	
	.tabs {
		display: flex;
		height: 88rpx;
		background-color: #fff;
		margin-top: 20rpx;
	}

	.tab-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative;
	}

	.tab-item text {
		font-size: 30rpx;
		color: #AFAFAF;
		font-weight: bold;
	}

	.tab-item.active text {
		color: #4095E5;
	}

	.tab-line {
		position: absolute;
		bottom: 0;
		width: 120rpx;
		height: 4rpx;
		background-color: #4095E5;
	}

	.content {
		flex: 1;
		overflow: auto;
		padding: 32rpx;
		background-color: #fff;
		margin-top: 8rpx;
	}

	.repair-detail {
		padding: 0 20rpx;
	}

	.detail-item {
		margin-bottom: 32rpx;
	}

	.detail-label {
		font-size: 16px;
		color: #707070;
		margin-right: 16rpx;
	}

	.detail-value {
		font-size: 16px;
		color: #000;
	}

	/*处理记录*/
	.processing-record {
		padding: 0 20rpx;
	}

	.record-item {
		position: relative;
		padding-left: 40rpx;
		margin-bottom: 40rpx;
	}

	.record-item::before {
		content: '';
		position: absolute;
		left: 0;
		top: 12rpx;
		width: 16rpx;
		height: 16rpx;
		background-color: #4095E5;
		border-radius: 50%;
	}

	.record-time {
		font-size: 12px;
		color: #AAAAAA;
		display: block;
		margin-bottom: 8rpx;
	}

	.record-action {
		font-size: 16px;
		color: #333;
		display: block;
	}
</style>